'''
Contains the code to create a GUI application that allows a user to create
a new non-CSN claim.
'''
import PySimpleGUI as sg
from warranty_manager.gui.chromebook_notes import get_chromebook_note_gui
from warranty_manager.claim.common import get_logger
from warranty_manager.claim.hp.non_csn.non_csn_claim import NonCSNClaim
from warranty_manager.claim.hp.non_csn.non_csn_claim_creator_layout import LAYOUT
from warranty_manager.gui.confirmation import get_confirmation


CLAIM_INFO = {
    'serial number': None,
    'title': None,
    'description': None,
    'troubleshooting': None,
}

WINDOW = sg.Window('HP Non-CSN Claim Creator', LAYOUT)

while True:
    event, values = WINDOW.read()

    if event in (None, 'Cancel'):
        break
    elif event == '-ADD_CHROMEBOOK_ISSUE-':
        try:
            description, troubleshooting = get_chromebook_note_gui()
            troubleshooting += ' Please dropship a box, to send unit to repair center.'
        except TypeError: # Chromebook issue GUI window was closed
            continue

        # To fill the title, description, and troubleshooting text into the form
        WINDOW['-TITLE-'].update(description)
        WINDOW['-DESCRIPTION-'].update(description)
        WINDOW['-TROUBLESHOOTING-'].update(troubleshooting)
    elif event == '-REMOVE_ISSUE-':
        if not values['-TITLE-'].strip() \
        and not values['-DESCRIPTION-'].strip() \
        and not values['-TROUBLESHOOTING-'].strip():
            sg.popup('There is no claim information to remove.', title='Info')
        else:
            confirmation = get_confirmation(
                'remove the title, description, and '
                'troubleshooting information from the claim'
            )

            if confirmation:
                WINDOW['-TITLE-'].update('')
                WINDOW['-DESCRIPTION-'].update('')
                WINDOW['-TROUBLESHOOTING-'].update('')
                sg.popup(
                    'The title, description, and troubleshooting steps have all been removed.',
                    title='Info Message'
                )
    elif event == 'Create Non-CSN Claim':
        sn = values['-SERIAL_NUMBER-'].strip()
        title = values['-TITLE-'].strip()
        description = values['-DESCRIPTION-'].strip()
        troubleshooting = values['-TROUBLESHOOTING-'].strip()

        if sn:
            CLAIM_INFO['serial number'] = sn
        else:
            sg.popup_error('You must enter a serial number!', title='Error Message')
            continue

        if title:
            CLAIM_INFO['title'] = title
        else:
            sg.popup_error('You must enter a title for the claim!', title='Error Message')
            continue

        if description:
            CLAIM_INFO['description'] = description
        else:
            sg.popup_error('You must enter a description for the claim!', title='Error Message')
            continue

        if troubleshooting:
            CLAIM_INFO['troubleshooting'] = troubleshooting
        else:
            sg.popup_error(
                'You must enter troubleshooting steps for the claim!',
                title='Error Message'
            )
            continue

        try:
            confirmation = get_confirmation('create a new, non-CSN HP warranty claim')

            if confirmation:
                CLAIM = NonCSNClaim(
                    {
                        'url': 'https://mycrm.support.hp.com/en-US/SignIn?'
                               'ReturnUrl=%2Fen-US%2Fcreate-case%2F',
                        **CLAIM_INFO,
                        'case id': None
                    }
                )
                CLAIM.create()

                # To clear the only field that must change with each claim
                WINDOW['-SERIAL_NUMBER-'].update('')

        except CLAIM.get_network_exceptions():
            continue
        except Exception as err:
            sg.popup_error(
                'An unexpected error has occurred. '
                'Please check the logs for more information.',
                title='Error Message'
            )
            get_logger('non_csn_claim_creator').exception(err)
    else:
        continue

WINDOW.close()
